<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>求阶乘</title>
	<style>*{font-size: 40px;}</style>
</head>
<body>
输入数值:<input type="text" id="num">(会自动取整)<hr>
<button onclick="sum()">计算</button>
<div id="factsum">结果为</div>
<script>
	function sum () {
		var x = document.getElementById('num').value;
		var fs = document.getElementById('factsum');
		x = parseInt(x);
		if (isNaN(x)) {return alert('输入非法!')}
		if (x<0) {return alert('不得小于零!')}
		if (x==0) {return alert('不得等于零!')}
		// console.log(x);
		fs.innerHTML = fact(x);
	}

	function fact (x) {
		// console.log(x);
		if (x==1) {
			return 1;
		}else{
			return x*fact(x-1);
		}
	}
	
</script>
	
</body>
</html>